package com.galudisu

import com.typesafe.config
import com.typesafe.config.{ConfigException, ConfigFactory}

trait Config {

  implicit class WrappedConfig(config: com.typesafe.config.Config) {
    def get(key: String, orElse: => String): String =
      try {
        config.getString(key)
      } catch {
        case ce: ConfigException => orElse
      }
  }

  protected lazy val appConfig: config.Config =
    try {
      ConfigFactory.load
    } catch {
      case ce: ConfigException => throw new AsyncPoolsException("Configuration failed to load", ce)
    }

  private val configRoot = "asyncpools"

  def poolConfig(name: String): config.Config =
    try {
      appConfig.getConfig(s"${configRoot }.${name }")
    } catch {
      case ce: ConfigException =>
        throw new AsyncPoolsException(s"Missing configuration of pool $name", ce)
    }

}